/**
 * Copyright (C) 2008 Motudo Studio
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package motudo.screen;

import motudo.screen.domain.FourNextTimer;
import motudo.screen.domain.INextTimer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;

/**
 * @author game@motudo.com (motudo)
 * 
 */
public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

	/**
	 * @param configurer
	 */
	public ApplicationWorkbenchWindowAdvisor(
			IWorkbenchWindowConfigurer configurer) {
		super(configurer);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.application.WorkbenchWindowAdvisor#createActionBarAdvisor(org.eclipse.ui.application.IActionBarConfigurer)
	 */
	public ActionBarAdvisor createActionBarAdvisor(
			IActionBarConfigurer configurer) {
		return new ApplicationActionBarAdvisor(configurer);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.application.WorkbenchWindowAdvisor#preWindowOpen()
	 */
	public void preWindowOpen() {
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		// small window size
		configurer.setInitialSize(new Point(20, 55));
		
		// large testing window size
		//configurer.setInitialSize(new Point(200, 200));
		configurer.setShowCoolBar(false);
		configurer.setShowStatusLine(false);
		configurer.setShowMenuBar(false); // remove menu bar

		// only work in the create time
		configurer.setShellStyle(SWT.ON_TOP | SWT.CLOSE | SWT.TITLE);
		configurer.setTitle("Motudo");
	}

	@Override
	public void postWindowOpen() {
		super.postWindowOpen();
		this.timerFullScreen();
	}

	public void timerFullScreen() {
		
		final INextTimer ntimer = Activator.getInjector().getInstance(INextTimer.class);
		
		final Display display = Display.getCurrent();
		final Shell shell = display.getActiveShell();
		
		//Color red = display.getSystemColor(SWT.COLOR_RED);
		Color black = display.getSystemColor(SWT.COLOR_BLACK);
		shell.setBackground(black);

		display.timerExec(ntimer.nextMillSecs(), new Runnable() {

			@Override
			public void run() {
				shell.setFullScreen(!shell.getFullScreen());
				shell.forceActive(); // always on top
				display.timerExec(ntimer.nextMillSecs(), this);
				//System.out.println("FULLSCREEN ? " + shell.getFullScreen() + ", NOW = " + new Date());
			}
		});
	}
}
